`
https://leetcode.cn/problems/sum-of-nodes-with-even-valued-grandparent/
`

/**
 * Definition for a binary tree node.
 * function TreeNode(val, left, right) {
 *     this.val = (val===undefined ? 0 : val)
 *     this.left = (left===undefined ? null : left)
 *     this.right = (right===undefined ? null : right)
 * }
 */
/**
 * @param {TreeNode} root
 * @return {number}
 */
var sumEvenGrandparent = function (root) {
  let sum = 0

  const traverse = (root) => {
    if (root === null) return

    // 如果当前结点值是偶数
    if (root.val % 2 === 0) {
      // 找到所有孙子结点，并将它们的值累加到结果上
      if (root.left) {
        if (root.left.left) sum += root.left.left.val
        if (root.left.right) sum += root.left.right.val
      }
      if (root.right) {
        if (root.right.left) sum += root.right.left.val
        if (root.right.right) sum += root.right.right.val
      }
    }

    traverse(root.left)
    traverse(root.right)
  }

  traverse(root)

  return sum
};